package io.github.mengfly.kvstore.util;

import java.util.concurrent.locks.Lock;

/**
 * @author wangp
 */
public class LockUtil {

    public static void onLock(Lock lock, RunnableE runnable) throws Exception {
        lock.lock();
        try {
            runnable.run();
        } finally {
            lock.unlock();
        }
    }

    public static  <T> T onLock(Lock lock, SupplierE<T> supplier) throws Exception {
        lock.lock();
        try {
            return supplier.get();
        } finally {
            lock.unlock();
        }
    }


    public interface SupplierE<T> {
        T get() throws Exception;
    }

    public interface RunnableE {
        public void run() throws Exception;

        ;
    }
}
